// Problem: P1868 饥饿的奶牛
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1868#submit
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

int n;
int dp[150005];

struct unit{
	int head,tail,val;
}nums[150005];

int lowerbound(int l,int r,int maxN)
{
    int ans=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(nums[mid].tail<maxN) 
        {
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    return ans;
}

inline bool cmp(unit a,unit b)
{
	return a.tail<b.tail;
}

int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>nums[i].head>>nums[i].tail;
        nums[i].val=nums[i].tail-nums[i].head+1;
    }
    sort(nums+1,nums+n+1,cmp);
    for(int i=1;i<=n;++i)
    {
        dp[i]=max(dp[i-1],dp[lowerbound(1,i-1,nums[i].head)]+nums[i].val);
    }
    cout<<dp[n];
    return 0;
}